package com.rk.admin.controller;
import com.rk.admin.aop.SystemControllerLog;
import com.rk.admin.domain.Images;
import com.rk.admin.service.HomeImgService;
import com.rk.admin.tools.CommonResult;
import com.rk.admin.tools.SysConstant;
import com.rk.admin.utils.FileNameUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Api(tags = "轮播图")
@RestController
@RequestMapping("/HomeImg")
@CrossOrigin //前后端分离配置跨域访问
public class HomeImgController {

    @Autowired
    private HomeImgService homeImgService;

    @Value("${environment.system}")
    private String system;
    

    /**
     * 加载轮播图
     * @return
     */
    @GetMapping("/findHomeImg")
    @ApiOperation(("查询首页轮播图"))
    public CommonResult list(){
        List<Images> imagelist = homeImgService.findHomeImg();
        return CommonResult.success(imagelist);
    }



    @ApiOperation("上传轮播图")
    @PostMapping("/addhomeImg")
    @SystemControllerLog(description ="添加轮播图")
    public CommonResult uploadhomeImg(MultipartFile file) {

        //获取上传的文件的名字
        String originalFilename = file.getOriginalFilename();
        String savefileName="";
        if (!file.isEmpty()) {//如果上传的头像不为空
            //存储头像到tecaherHeadImg目录下
            //获取文件原来的名字
            //获取文件类型
            String fileType = FileNameUtil.getFileType(originalFilename);
            //更改文件存储的名称，避免上传的文件名重复而覆盖
            savefileName = FileNameUtil.getUUIDFileName() + fileType;

            String savePath="";
            //获取项目根路径
            String path = System.getProperty("user.dir");

            if("linux".equals(system)){
                savePath= SysConstant.HOMEIMG_LINUX;
            }else if("windows".equals(system)){
                savePath = path+ SysConstant.HOMEIMG_WINDOWS;
            }


            //头像存储路径
            File realpath=new File(savePath);
            //如果路径不存在，就创建路径，上传到服务器部署的时候，肯定没有这个路径存在的
            if(!realpath.exists())
            {
                realpath.mkdirs();
            }

            //文件转存
            try {
                file.transferTo(new File(realpath.getAbsolutePath() + File.separator + savefileName));
                System.out.println("存储路径:" + realpath.getAbsolutePath() + File.separator + savefileName);
            } catch (IOException e) {
                e.printStackTrace();
                return CommonResult.failed("轮播图上传失败!");
            }
        }
        return CommonResult.success("/lunImg/" + savefileName, "轮播图上传成功!");
    }


    @ApiOperation("保存轮播图")
    @PostMapping("/saveImg")
    public CommonResult saveImg(@RequestParam("url") String url) {
        System.out.println("前端传递过来的轮播图"+url);

        //将路径入数据库
        Images images=new Images();
        images.setImagpath(url);
        Boolean flag = homeImgService.addHomeImg(images);
        if(!flag){//添加失败
            return CommonResult.failed("轮播图保存失败!");
        }
        return CommonResult.success(images);
    }


    @ApiOperation("删除轮播图")
    @PostMapping()
    @SystemControllerLog(description ="删除轮播图")
    public CommonResult deleteHomeImgbyid(@RequestParam("id") Integer id) {
        Boolean flag = homeImgService.deletebyid(id);
        if(flag){//删除成功
            return CommonResult.success(flag);
        }else {
            return CommonResult.failed("删除失败!");
        }
    }
}
